package com.manager.car.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.manager.car.po.Cars;
import com.manager.car.po.NewsInfo;
import com.manager.car.service.INewsService;
import com.manager.car.util.PageModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class NewsInfoAction extends ActionSupport{

	private INewsService dao;
	private NewsInfo news;
	private Integer id;
	private Integer typeid=0;
	private PageModel<NewsInfo> pageModel;
	private int page,limit;
	
	
	
	public void querybypage() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if(page==0) {
			page = 1;
		}
		if(limit==0) {
			limit=10;
		}
		PageModel<NewsInfo> pm = dao.queryall(page, limit,typeid);
		
		Gson gson = new Gson();
		String json = gson.toJson(pm);
		
		out.print(json);
		out.flush();
		out.close();
	}
	
	
	public void dele() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		dao.delete(id);
		int i=-1;
		if(dao.get(id)==null)
			i=1;
		else
			i=0;
		out.print(i);
		out.flush();
		out.close();
	}
	
	
	public String findall() {
		List<NewsInfo> list=dao.queryAll(typeid);
		ActionContext.getContext().put("list_news", list);
		ActionContext.getContext().put("msg","list");
		return "list_news";
	}
	
	public String queryall() {
		List<NewsInfo> list=dao.queryAll(typeid);
		ActionContext.getContext().put("list_news", list);
		ActionContext.getContext().put("msg","list");
		return "all_news";
	}
	
	public String add() {
		dao.add(news);
		return "ok_news";
	}
	
	public String upda() {
		dao.update(news);
		return "ok_news";
	}
	
	public String del() {
		dao.delete(id);
		return "ok_news";
	}
	
	public String get() {
		NewsInfo news = dao.get(id);
		ActionContext.getContext().put("news", news);
		ActionContext.getContext().put("msg", "news");
		return "list_news";
	}
	
	public String edit() {
		NewsInfo news = dao.get(id);
		ActionContext.getContext().put("news", news);
		return "edit_news";
	}
	
	
	public INewsService getDao() {
		return dao;
	}

	public void setDao(INewsService dao) {
		this.dao = dao;
	}

	public NewsInfo getNews() {
		return news;
	}

	public void setNews(NewsInfo news) {
		this.news = news;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getTypeid() {
		return typeid;
	}

	public void setTypeid(Integer typeid) {
		this.typeid = typeid;
	}

	public PageModel<NewsInfo> getpageModel() {
		return pageModel;
	}

	public void setpageModel(PageModel<NewsInfo> pageModel) {
		this.pageModel = pageModel;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
	
	
}
